﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MBS.UserControls.MovieScheduleUserControl
{
    public partial class ViewMovieScheduleUserControl : System.Web.UI.UserControl
    {

       
        Dictionary<int, string> cinemaDictionary;
        Dictionary<int, int> screenDictionary;
        Dictionary<int, string> movieDictionary;


        MBSBLC.BLC blc = new MBSBLC.BLC();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
             
                cinemaDictionary = blc.GetAllCinemas();
                CinemaDropDownList.DataSource = cinemaDictionary;
                CinemaDropDownList.DataTextField = "Value";
                CinemaDropDownList.DataValueField = "Key";
                CinemaDropDownList.DataBind();

                movieDictionary = blc.GetAllMovies();
                MovieDropDownList.DataSource = movieDictionary;
                MovieDropDownList.DataTextField = "Value";
                MovieDropDownList.DataValueField = "Key";
                MovieDropDownList.DataBind();

                if (CinemaDropDownList.SelectedValue.Equals(""))
                {
                    screenDictionary = new Dictionary<int, int>();
                }
                else
                {
                    screenDictionary = blc.GetAllScreensForACinema(Convert.ToInt32(CinemaDropDownList.SelectedValue));
                }
                ScreenDropDownList.DataSource = screenDictionary;
                ScreenDropDownList.DataTextField = "Value";
                ScreenDropDownList.DataValueField = "Key";
                ScreenDropDownList.DataBind();

                LoadMovieSchedule();
            }
        }

        private void LoadMovieSchedule()
        {

            int MovieID = Convert.ToInt32(MovieDropDownList.SelectedValue.Equals("") ? "0" : MovieDropDownList.SelectedValue);
            int CinemaID = Convert.ToInt32(CinemaDropDownList.SelectedValue.Equals("") ? "0" : CinemaDropDownList.SelectedValue);
            int ScreenNo = Convert.ToInt32(ScreenDropDownList.SelectedValue.Equals("") ? "0" : ScreenDropDownList.SelectedValue);
            MovieScheduleGridView.DataSourceID = null;
            MovieScheduleGridView.DataSource = blc.GetMovieScheduleByMovieAndCinemaAndTheatreNo(MovieID, CinemaID, ScreenNo);
            MovieScheduleGridView.DataBind();

        }

        protected void ScreenDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadMovieSchedule();
        }

        protected void CinemaDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadMovieSchedule();
        }

        protected void MovieDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadMovieSchedule();
        }


        protected void MovieScheduleGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int movieSchedule = Convert.ToInt32(MovieScheduleGridView.Rows[e.RowIndex].Cells[0].Text);
            blc.DeleteMovieSchedule(movieSchedule);
            LoadMovieSchedule();
        }


    }
}